<?php
if($this->do=='comment'){
	$this->check_access('content_comment');
	$sql="SELECT * FROM ".DB_PREFIX."content_comment WHERE 1=1";
	if(!empty($_GET['article_id'])){
		$sql.=" AND article_id='".intval($_GET['article_id'])."'";
	}
	$sql.=" AND parent_id=0 ORDER BY comment_id DESC";
	$page_size=$this->config['content_admin_comment_list_size']?$this->config['content_admin_comment_list_size']:20;
	$page_current=isset($_GET['page'])&&is_numeric($_GET['page'])?intval($_GET['page']):1;
	$count=$this->db->count($sql);
	$res=$this->db->result($sql." limit ".(($page_current-1)*$page_size).",".$page_size);
	$array=array();
	$pager=array();
	if($count>0){
		foreach($res as $row){
			$array[$row['comment_id']]['id']=$row['comment_id'];
			$array[$row['comment_id']]['name']=$row['comment_name'];
			$array[$row['comment_id']]['content']=$row['comment_content'];
			$array[$row['comment_id']]['reply']=$row['comment_reply'];
			$array[$row['comment_id']]['email']=$row['comment_email'];
			$array[$row['comment_id']]['site']=$row['comment_site'];
			$array[$row['comment_id']]['ip']=$row['comment_ip'];
			$array[$row['comment_id']]['ip_address']=get_ip_address($row['comment_ip']);
			$array[$row['comment_id']]['agent']=$row['comment_agent'];
			$array[$row['comment_id']]['time']=date("Y.m.d H:i:s",$row['comment_time']);
			$array[$row['comment_id']]['is_display']=$row['comment_is_display'];
			$array[$row['comment_id']]['article_id']=$row['article_id'];
		}
		$parameters="action=content&do=comment&";
		if(!empty($_GET['article_id'])){
			$parameters.="article_id=".intval($_GET['article_id'])."&";
		}
		$pager=pager(get_self(),$parameters,$page_current,$page_size,$count);
	}
	$this->template->in('comment',$array);
	$this->template->in('pagebar',$pager);
	$this->template->out('content.comment.list.php');
}
if($this->do=='comment_edit'){#编辑评论
	$comment_id=isset($_GET['comment_id'])?intval($_GET['comment_id']):'';
	$row=$this->db->row("SELECT * FROM ".DB_PREFIX."content_comment WHERE comment_id='$comment_id'");
	$comment=array();
	$comment['id']=$row['comment_id'];
	$comment['name']=$row['comment_name'];
	$comment['article_title']=$this->db->value(DB_PREFIX."content_article","article_title","article_id=".$row['article_id']);
	$comment['ip']=$row['comment_ip'];
	$comment['ip_address']=get_ip_address($row['comment_ip']);
	$comment['content']=$row['comment_content'];
	$comment['reply']=$row['comment_reply'];
	$comment['email']=$row['comment_email'];
	$comment['site']=$row['comment_site'];
	$comment['time']=date("Y-m-d h:i:s",$row['comment_time']);
	$comment['is_display']=$row['comment_is_display'];
	$comment['article_id']=$row['article_id'];
	$array=array();
	$sql="SELECT * FROM ".DB_PREFIX."content_comment WHERE parent_id=".$row['comment_id'];
	$res=$this->db->result($sql);
	foreach($res as $row){
		$array[$row['comment_id']]['id']=$row['comment_id'];
		$array[$row['comment_id']]['name']=$row['comment_name'];
		$array[$row['comment_id']]['content']=$row['comment_content'];
		$array[$row['comment_id']]['reply']=$row['comment_reply'];
		$array[$row['comment_id']]['email']=$row['comment_email'];
		$array[$row['comment_id']]['site']=$row['comment_site'];
		$array[$row['comment_id']]['ip']=$row['comment_ip'];
		$array[$row['comment_id']]['ip_address']=get_ip_address($row['comment_ip']);
		$array[$row['comment_id']]['agent']=$row['comment_agent'];
		$array[$row['comment_id']]['time']=date("Y.m.d H:i:s",$row['comment_time']);
		$array[$row['comment_id']]['is_display']=$row['comment_is_display'];
		$array[$row['comment_id']]['article_id']=$row['article_id'];
		$array[$row['comment_id']]['page_id']=$row['page_id'];
	}
	$this->template->in('comment',$comment);
	$this->template->in('array',$array);
	$this->template->in('mode','update');
	$this->template->out('content.comment.info.php');
}
if($this->do=='comment_update'){#更新评论
	$this->check_access('content_comment');
	$article_id=empty($_POST['article_id'])?0:intval($_POST['article_id']);
	$comment_id=empty($_POST['comment_id'])?0:intval($_POST['comment_id']);
	$comment_content=empty($_POST['comment_content'])?'':addslashes(trim($_POST['comment_content']));
	$comment_reply=empty($_POST['comment_reply'])?'':addslashes(trim($_POST['comment_reply']));
	$comment_is_display=empty($_POST['comment_is_display'])?0:1;
	$array=array();
	$array['comment_content']=$comment_content;
	$array['comment_reply']=$comment_reply;
	$array['comment_is_display']=$comment_is_display;
	$this->db->update(DB_PREFIX."content_comment",$array,"comment_id=$comment_id");
	if(!empty($_POST['comment_is_email'])){
		$to=trim($_POST['comment_email']);
		$subject="您在[".$this->config['title']."]的评论已经回复！";

		$article=$content->get_article_info($article_id);

		$content="评论文章：<a href='http://".$_SERVER['SERVER_NAME'].PATH.HTML_DIR.$content->category_parent_path($article['category_id'])."/".$article['html'].".html'>".$article['title']."</a><br />";
		$content.="您的评论：".$comment_content."<br />";
		$content.="管理回复：".$comment_reply."<br />";
		$subject=iconv('UTF-8','GB2312',$subject);
		$content=iconv('UTF-8','GB2312',$content);
		import('smtp');
		$smtp=new smtp($this->config['smtp_server'],$this->config['smtp_port'],true,$this->config['smtp_user'],$this->config['smtp_password']);
		$smtp->send($to,$this->config['smtp_mail'],$subject,$content,'HTML');
	}
	$this->html_index();
	redirect('?action=content&do=comment');
}
if($this->do=='comment_delete'){#删除内容
	$this->check_access('content_comment');
	$comment_id=empty($_POST['comment_id'])?array():$_POST['comment_id'];
	foreach($comment_id as $value){
		if(!empty($value)){
			$row=$this->db->row("SELECT * FROM ".DB_PREFIX."content_comment WHERE comment_id='$value'");
			$this->db->query("UPDATE ".DB_PREFIX."content_article SET article_comment_count=article_comment_count-1 WHERE article_id=".$row['article_id']."");
			$this->db->delete(DB_PREFIX."content_comment","comment_id=".$value);
		}
	}
	$this->html_index();
	redirect('?action=content&do=comment');
}